package org.goldfish.message_board01.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class DatabaseConnectionManager {
    private final String dbUrl = "jdbc:mysql://localhost:3306/cookied_chatroom";
    private final String dbUser = "root";
    private final String dbPassword = "A7btngqx";
    private final int MAX_POOL_SIZE = 5;

    private final BlockingQueue<Connection> connectionPool = new ArrayBlockingQueue<>(MAX_POOL_SIZE);
    private final List<Connection> usedConnections = new ArrayList<>();

    public DatabaseConnectionManager() {
        try {
            for (int i = 0; i < MAX_POOL_SIZE; i++) {
                connectionPool.add(createConnection());
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("Error initializing the connection pool", e);
        }
    }

    /**
     * Fetches a connection from the pool.
     * @return A database connection.
     * @throws SQLException If no available connections.
     */
    public synchronized Connection getConnection() throws SQLException {
        if (connectionPool.isEmpty()) {
            throw new SQLException("No available connections in the pool");
        }
        Connection connection = null;
        try {
            connection = connectionPool.take();
            System.out.println("Connection is taken from pool");
            System.out.println(connectionPool.size());
        } catch (InterruptedException e) {
            throw new SQLException("Error fetching connection from pool", e);
        }
//        usedConnections.add(connection);
        return connection;
    }

    /**
     * Releases a connection back to the pool.
     * @param connection The connection to release.
     */
    public synchronized void releaseConnection(Connection connection) {
        usedConnections.remove(connection);
        try {
            connectionPool.put(connection);
        } catch (InterruptedException e) {
            e.printStackTrace(System.err);
        }
    }

    /**
     * Closes all connections in the pool.
     */
    public synchronized void closeAllConnections() {
        for (Connection connection : connectionPool) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        connectionPool.clear();
    }

    /**
     * Creates a new database connection.
     * @return A new database connection.
     * @throws SQLException If connection creation fails.
     */
    private Connection createConnection() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new SQLException("MySQL JDBC Driver not found", e);
        }
        return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
    }
}

